package com.example.mkio.androidpractice.Views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import com.example.mkio.androidpractice.R;

/**
 * Created by mk.io on 17-5-28.
 */

public class CustomViewSecond extends View {

    private static final String TAG = "CustomViewSecond";

    private int titleSize;
    private int titleColor;
    private String titleText;
    private Bitmap mImage;
    private int imageScale;

    private Paint mPaint;
    private Rect mTextContentBoundRect;
    private Rect rect;


    /**
     * 图片的缩放模式
     */
    private static final int IMAGE_SCALE_FITXY = 0;
    private static final int IMAGE_SCALE_CENTER = 1;

    int realHeight = 0;
    int realWidth = 0;

    public CustomViewSecond(Context context) {
        this(context, null);
    }

    public CustomViewSecond(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomViewSecond(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomViewSecond, defStyleAttr, 0);
        int n = typedArray.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = typedArray.getIndex(i);
            switch (attr) {
                case R.styleable.CustomViewSecond_imageSecond:
                    mImage = BitmapFactory.decodeResource(getResources(), typedArray.getResourceId(attr, 0));
                    break;
                case R.styleable.CustomViewSecond_imageScaleTypeSecond:
                    imageScale = typedArray.getInt(attr, 0);
                    break;
                case R.styleable.CustomViewSecond_titleTextSecond:
                    titleText = typedArray.getString(attr);
                    break;
                case R.styleable.CustomViewSecond_titleTextColorSecond:
                    titleColor = typedArray.getColor(attr, Color.RED);
                    break;
                case R.styleable.CustomViewSecond_titleTextSizeSecond:
                    titleSize = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                            16, getResources().getDisplayMetrics()));

                    break;
            }
        }
        typedArray.recycle();
        mPaint = new Paint();
        rect = new Rect();
        mTextContentBoundRect = new Rect();

        mPaint.setTextSize(titleSize);
        mPaint.getTextBounds(titleText, 0, titleText.length(), mTextContentBoundRect);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int specWidth = MeasureSpec.getSize(widthMeasureSpec);
        int specHeight = MeasureSpec.getSize(heightMeasureSpec);
        Log.e(TAG, "onMeasure: specWidth:"+ specWidth+",specHeight:"+specHeight);
        int specHeightMode = MeasureSpec.getMode(heightMeasureSpec);
        int specWidthMode = MeasureSpec.getMode(heightMeasureSpec);

        if (specHeightMode == MeasureSpec.EXACTLY) {
            realHeight = specHeight;
        } else {
            int dependImagHeight = getPaddingBottom() + getPaddingTop() + mImage.getHeight() + mTextContentBoundRect.height();
            if (specHeightMode == MeasureSpec.AT_MOST) {
                realHeight = Math.min(specHeight, dependImagHeight);
            }
        }

        if (specWidthMode == MeasureSpec.EXACTLY) {
            realWidth = specWidth;
        } else {
            int denpendImageWidth = getPaddingLeft() + getPaddingRight() + mTextContentBoundRect.width();
            if (specWidthMode == MeasureSpec.AT_MOST) {
                realWidth = Math.min(denpendImageWidth, specWidth);
            }
        }
        Log.e(TAG, "onMeasure: "+ "realWidth:"+realWidth+",realHeight"+realHeight);
        setMeasuredDimension(realWidth, realHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.RED);
        Log.e(TAG, "onDraw: "+ "getMeasuredWidth:"+getMeasuredWidth()+",getMeasuredHeight:"+getMeasuredHeight());
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);

        rect.left = getPaddingLeft();
        rect.right = realWidth - getPaddingRight();
        rect.top = getPaddingTop();
        rect.bottom = realHeight - getPaddingBottom();

        mPaint.setColor(titleColor);
        mPaint.setStyle(Paint.Style.FILL);


        /**
         * mTextContentBoundRect.width() 该变量的值为文字的长度 ,mTextContentBoundRect 的高度为 文字的高度
         * mPaint.getTextBounds(titleText, 0, titleText.length(), mTextContentBoundRect); 在此处赋值，（待确定）
         */
        if (mTextContentBoundRect.width() > realWidth) {
            TextPaint paint = new TextPaint(mPaint);
            String mst = TextUtils.ellipsize(titleText, paint, realWidth - getPaddingLeft() - getPaddingRight(), TextUtils.TruncateAt.END).toString();
            canvas.drawText(mst, getPaddingLeft(), realHeight - getPaddingBottom(), mPaint);
        } else {
            canvas.drawText(titleText, realWidth / 2 - mTextContentBoundRect.width() / 2 * 1.0f, realHeight - getPaddingBottom(), mPaint);
        }
        rect.bottom -= mTextContentBoundRect.height();//确定图片左下方的位置(在原来的基础上减去文字的高度)
        if (imageScale == IMAGE_SCALE_FITXY) {
            canvas.drawBitmap(mImage, null, rect, mPaint);
        } else {
            //计算居中的矩形范围
            rect.left = realWidth / 2 - mImage.getWidth() / 2;
            rect.right = realWidth / 2 + mImage.getWidth() / 2;
            rect.top = (realHeight - mTextContentBoundRect.height() - mImage.getHeight()) / 2;
            rect.bottom = (realHeight - mTextContentBoundRect.height()) / 2 + mImage.getWidth() / 2;
            canvas.drawBitmap(mImage, null, rect, mPaint);
        }
    }
}
